package com.example.news.controller;

import com.example.news.pojo.User;
import com.example.news.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //展示所有用户
    @RequestMapping("/all_users")
    public String getAllUser(Model model){
        List<User> userList = userService.getAllUser();
        System.out.println(userList.toString());
        model.addAttribute("userList",userList);
        return "user_list";
    }

    //投票
    @ResponseBody
    @RequestMapping("/add_count")
    public Map<String,Object> addCount(Integer uid){
        Map<String, Object> map = new HashMap<>();
        Integer count = userService.addCount(uid);
        map.put("count", count);
        map.put("uid", uid);
        map.put("success", true);
        return map;
    }

    //排序
    @RequestMapping("/sort")
    public void sortUser(){
        /**方式一：
         * 1.从redis的hash中取出uid和票数存放到zset里
         * 2.zset倒序排序并取出uid顺序
         * 3.根据排序后的uid顺序获取每一个实例对象，并将对象存入List集合返回
         * 方式二:
         * 1.将实例对象除票数外的属性拼接成一个字符串，把该字符串和票数存入zet
         * 2.zset倒序排序即最终结果
         * (字符串拼接时需要拼一些特殊符号)
         */

    }
}
